<?php
/**
 * Created by JetBrains PhpStorm.
 * User: AndreyG
 * Date: 24.01.12
 * Time: 15:01
 * To change this template use File | Settings | File Templates.
 */
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Задание #1</title>
    <style type="text/css">
        table {text-align: left; border: 1px solid #bbb;}
        table td {border: 1px solid #bbb;}
        .button {display: block; float: left; background: #ddd; padding: 5px; margin: 10px;}
    </style>
    <script type="text/javascript">
        function setLocation(param, value) {
            var url = window.location.href;
            //var url = 'topdetal.ru/index.php?order=8&dir=desc';

            if(-1 == (url.indexOf('?'))) {
                return '?' + param + '=' + value;
            }
            if (-1 < (pos = url.indexOf(param + '='))) {
                return url.replace(new RegExp (param+'=[0-9a-z]*', 'gi'), param + '=' + value);
            } else {
                return url + '&' + param + '=' + value;
            }
        }
    </script>
</head>
<body>
<h1>Задание #1: Функциональная реализация проекта</h1>
<table>
    <tr>
        <th colspan="4">Фильтры</th>
        <th>
            <select name="cat" onchange="window.location.href = setLocation(this.getAttribute('name'), this.value)">
                <option></option>
                <option value="">- Показать все -</option>
                <?php
                    while(list($key, $category) = each($categoryCollection)):
                ?>
                <option value="<?php echo $category['id']; ?>"><?php echo $category['title']; ?></option>
                <?php
                    endwhile;
                ?>
            </select>
        </th>
        <th>
            <select name="location" onchange="window.location.href = setLocation(this.getAttribute('name'), this.value)">
                <option></option>
                <option value="">- Показать все -</option>
                <?php
                    while(list($key, $location) = each($locationCollection)):
                ?>
                <option value="<?php echo $location['id']; ?>"><?php echo $location['title']; ?></option>
                <?php
                    endwhile;
                ?>
            </select>
        </th>
        <th>
            <select name="sort" onchange="window.location.href = setLocation(this.getAttribute('name'), this.value)">

                <option value="name" <?php if($params['sort'] == 'name') echo 'selected';?>> По названию</option>
                <option value="rating" <?php if($params['sort'] == 'rating') echo 'selected';?>> По рейтингу</option>
            </select>
            <?php
                if(!$params['dir'] || $params['dir'] == 'asc'):
            ?>
            <a href="#" onclick="window.location.href = setLocation('dir', 'desc')">< &darr; ></a>
            <?php
                elseif($params['dir'] == 'desc'):
            ?>
            <a href="#" onclick="window.location.href = setLocation('dir', 'asc')">< &uarr; ></a>
            <?php
                endif;
            ?>
        </th>
    </tr>
    <tr>
        <td><strong>Рейтинг</strong></td>
        <td><strong>Компания</strong></td>
        <td><strong>Описание</strong></td>
        <td><strong>Телефон</strong></td>
        <td><strong>Email</strong></td>
        <td><strong>Город</strong></td>
        <td><strong>Категории</strong></td>
    </tr>

    <?php while(list($key, $company) = each($companyCollection)): ?>
        <tr>
            <td><?php echo $company['rating']; ?></td>
            <td><?php echo $company['name']; ?></td>
            <td><?php echo $company['short_desc']; ?></td>
            <td><?php echo $company['phone']; ?></td>
            <td><?php echo $company['email']; ?></td>
            <td><?php echo $company['location']; ?></td>
            <td><?php echo $company['category']; ?></td>
        </tr>
    <?php endwhile; ?>
    <tr>
        <td colspan="7">
            <a href="add.php" class="button">Добавить компанию</a>
            <a href="list.php" class="button">Сбросить фильтры</a>
        </td>
    </tr>
</table>

</body>
</html>
